<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <h1>聊天室</h1>
    <input id="sendTxt" type="text">
    <button id="sendBtn">发送</button>
    <div id="recv"></div>
    <script>
        var websocket = new WebSocket('ws://localhost:3000/')
        function showMessage(str) {
            var div = document.createElement('div')
            div.innerHTML = str
            document.body.appendChild(div)
        }
        websocket.onopen = function () {
            console.log('websoket open')
            document.getElementById('sendBtn').onclick = function () {
                var txt = document.getElementById("sendTxt").value
                if (txt) {
                    websocket.send(txt)
                }
            }
        }
        websocket.onclose = function () {
            console.log('websoket已关闭')
        }
        // 接收数据
        websocket.onmessage = function (e) {
            console.log(e.data)
            showMessage(e.data)
        }


    </script>
</head>

<body>

</body>

</html>